import request from '@/utils/request'

// Mock数据
const mockShipmentList = [
  {
    id: '1',
    shipmentCode: 'YD202401150001',
    status: '1',
    statusText: '运输中',
    destinationProvince: '广东省',
    destinationCity: '深圳市',
    destinationDistrict: '南山区',
    receiverContact: '张三',
    receiverPhone: '13800138001',
    currentStatus: {
      currentCity: '东莞市',
      delayStatus: '正常',
      goodsCondition: '完好',
      handoverStatus: '已交接'
    },
    createTime: '2024-01-15 08:30:00',
    updateTime: '2024-01-16 14:20:00'
  },
  {
    id: '2',
    shipmentCode: 'YD202401150002',
    status: '2',
    statusText: '派送中',
    destinationProvince: '江苏省',
    destinationCity: '南京市',
    destinationDistrict: '鼓楼区',
    receiverContact: '李四',
    receiverPhone: '13800138002',
    currentStatus: {
      currentCity: '南京市',
      delayStatus: '轻微延时',
      goodsCondition: '完好',
      handoverStatus: '待交接'
    },
    createTime: '2024-01-15 09:15:00',
    updateTime: '2024-01-16 16:45:00'
  },
  {
    id: '3',
    shipmentCode: 'YD202401150003',
    status: '3',
    statusText: '已签收',
    destinationProvince: '浙江省',
    destinationCity: '杭州市',
    destinationDistrict: '西湖区',
    receiverContact: '王五',
    receiverPhone: '13800138003',
    currentStatus: {
      currentCity: '杭州市',
      delayStatus: '正常',
      goodsCondition: '完好',
      handoverStatus: '已交接'
    },
    createTime: '2024-01-14 10:20:00',
    updateTime: '2024-01-16 11:30:00'
  },
  {
    id: '4',
    shipmentCode: 'YD202401150004',
    status: '1',
    statusText: '运输中',
    destinationProvince: '上海市',
    destinationCity: '上海市',
    destinationDistrict: '浦东新区',
    receiverContact: '赵六',
    receiverPhone: '13800138004',
    currentStatus: {
      currentCity: '苏州市',
      delayStatus: '严重延时',
      goodsCondition: '轻微损坏',
      handoverStatus: '交接异常'
    },
    createTime: '2024-01-15 11:45:00',
    updateTime: '2024-01-16 18:10:00'
  },
  {
    id: '5',
    shipmentCode: 'YD202401150005',
    status: '0',
    statusText: '待发货',
    destinationProvince: '北京市',
    destinationCity: '北京市',
    destinationDistrict: '朝阳区',
    receiverContact: '孙七',
    receiverPhone: '13800138005',
    currentStatus: {
      currentCity: '广州市',
      delayStatus: '正常',
      goodsCondition: '完好',
      handoverStatus: '待交接'
    },
    createTime: '2024-01-16 07:30:00',
    updateTime: '2024-01-16 07:30:00'
  }
]

const mockTrackingData = {
  '1': [
    {
      id: 't1',
      shipmentId: '1',
      timestamp: '2024-01-15 08:30:00',
      location: '广州仓库',
      status: 'shipped',
      statusText: '发货',
      operator: '仓库管理员',
      remark: '从广州仓库发出',
      longitude: 113.264385,
      latitude: 23.129163
    },
    {
      id: 't2',
      shipmentId: '1',
      timestamp: '2024-01-15 14:30:00',
      location: '东莞转运中心',
      status: 'transit',
      statusText: '运输',
      operator: '转运员A',
      remark: '到达东莞转运中心',
      longitude: 113.746262,
      latitude: 23.046237
    },
    {
      id: 't3',
      shipmentId: '1',
      timestamp: '2024-01-16 08:00:00',
      location: '东莞转运中心',
      status: 'transit',
      statusText: '运输',
      operator: '转运员B',
      remark: '从东莞转运中心发出',
      longitude: 113.746262,
      latitude: 23.046237
    },
    {
      id: 't4',
      shipmentId: '1',
      timestamp: '2024-01-16 14:20:00',
      location: '深圳配送站',
      status: 'delivering',
      statusText: '派送',
      operator: '配送员',
      remark: '到达深圳南山区配送站',
      longitude: 113.930000,
      latitude: 22.530000
    }
  ],
  '2': [
    {
      id: 't5',
      shipmentId: '2',
      timestamp: '2024-01-15 09:15:00',
      location: '广州仓库',
      status: 'shipped',
      statusText: '发货',
      operator: '仓库管理员',
      remark: '从广州仓库发出',
      longitude: 113.264385,
      latitude: 23.129163
    },
    {
      id: 't6',
      shipmentId: '2',
      timestamp: '2024-01-15 18:45:00',
      location: '南京转运中心',
      status: 'transit',
      statusText: '运输',
      operator: '转运员C',
      remark: '到达南京转运中心',
      longitude: 118.767413,
      latitude: 32.041544
    },
    {
      id: 't7',
      shipmentId: '2',
      timestamp: '2024-01-16 10:30:00',
      location: '南京转运中心',
      status: 'transit',
      statusText: '运输',
      operator: '转运员D',
      remark: '从南京转运中心发出',
      longitude: 118.767413,
      latitude: 32.041544
    },
    {
      id: 't8',
      shipmentId: '2',
      timestamp: '2024-01-16 16:45:00',
      location: '南京鼓楼区配送站',
      status: 'delivering',
      statusText: '派送',
      operator: '配送员',
      remark: '正在派送中，预计今日送达',
      longitude: 118.778074,
      latitude: 32.060255
    }
  ],
  '3': [
    {
      id: 't9',
      shipmentId: '3',
      timestamp: '2024-01-14 10:20:00',
      location: '广州仓库',
      status: 'shipped',
      statusText: '发货',
      operator: '仓库管理员',
      remark: '从广州仓库发出',
      longitude: 113.264385,
      latitude: 23.129163
    },
    {
      id: 't10',
      shipmentId: '3',
      timestamp: '2024-01-15 16:30:00',
      location: '杭州转运中心',
      status: 'transit',
      statusText: '运输',
      operator: '转运员E',
      remark: '到达杭州转运中心',
      longitude: 120.153576,
      latitude: 30.287459
    },
    {
      id: 't11',
      shipmentId: '3',
      timestamp: '2024-01-16 09:00:00',
      location: '杭州西湖区配送站',
      status: 'delivering',
      statusText: '派送',
      operator: '配送员',
      remark: '开始派送',
      longitude: 120.129720,
      latitude: 30.259244
    },
    {
      id: 't12',
      shipmentId: '3',
      timestamp: '2024-01-16 11:30:00',
      location: '收货地址',
      status: 'delivered',
      statusText: '签收',
      operator: '王五',
      remark: '客户已签收，货物完好',
      longitude: 120.129720,
      latitude: 30.259244
    }
  ]
}

// 运单综合查询接口
export function getShipmentComprehensive(query) {
  // 模拟API调用延迟
  return new Promise((resolve) => {
    setTimeout(() => {
      let filteredData = [...mockShipmentList]
      
      // 模拟搜索过滤
      if (query.shipmentCode) {
        filteredData = filteredData.filter(item => 
          item.shipmentCode.includes(query.shipmentCode)
        )
      }
      
      if (query.status) {
        filteredData = filteredData.filter(item => item.status === query.status)
      }
      
      if (query.destinationProvince) {
        filteredData = filteredData.filter(item => 
          item.destinationProvince.includes(query.destinationProvince)
        )
      }
      
      if (query.destinationCity) {
        filteredData = filteredData.filter(item => 
          item.destinationCity.includes(query.destinationCity)
        )
      }
      
      // 模拟分页
      const pageNum = query.pageNum || 1
      const pageSize = query.pageSize || 10
      const startIndex = (pageNum - 1) * pageSize
      const endIndex = startIndex + pageSize
      const paginatedData = filteredData.slice(startIndex, endIndex)
      
      resolve({
        code: 200,
        msg: 'success',
        data: {
          total: filteredData.length,
          rows: paginatedData
        }
      })
    }, 500)
  })
}

// 运单轨迹查询接口
export function getShipmentTracking(shipmentId, params = {}) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const shipmentInfo = mockShipmentList.find(item => item.id === shipmentId)
      const trackingList = mockTrackingData[shipmentId] || []
      
      if (params.viewMode === 'daily') {
        // 按天分组轨迹数据
        const dailyTracking = {}
        trackingList.forEach(track => {
          const date = track.timestamp.split(' ')[0]
          if (!dailyTracking[date]) {
            dailyTracking[date] = []
          }
          dailyTracking[date].push(track)
        })
        
        const dailyTrackingArray = Object.keys(dailyTracking).map(date => ({
          date,
          tracks: dailyTracking[date]
        }))
        
        resolve({
          code: 200,
          msg: 'success',
          data: {
            shipmentInfo,
            dailyTracking: dailyTrackingArray
          }
        })
      } else {
        // 总体轨迹
        resolve({
          code: 200,
          msg: 'success',
          data: {
            shipmentInfo,
            trackingList
          }
        })
      }
    }, 300)
  })
}

// 运单状态选项
export const shipmentStatusOptions = [
  { label: '全部', value: '' },
  { label: '待发货', value: '0' },
  { label: '运输中', value: '1' },
  { label: '派送中', value: '2' },
  { label: '已签收', value: '3' },
  { label: '异常', value: '4' }
]

// 延时状态选项
export const delayStatusOptions = [
  { label: '正常', value: '正常', type: 'success' },
  { label: '轻微延时', value: '轻微延时', type: 'warning' },
  { label: '严重延时', value: '严重延时', type: 'danger' }
]

// 货物状况选项
export const goodsConditionOptions = [
  { label: '完好', value: '完好', type: 'success' },
  { label: '轻微损坏', value: '轻微损坏', type: 'warning' },
  { label: '严重损坏', value: '严重损坏', type: 'danger' }
]

// 交接情况选项
export const handoverStatusOptions = [
  { label: '已交接', value: '已交接', type: 'success' },
  { label: '待交接', value: '待交接', type: 'warning' },
  { label: '交接异常', value: '交接异常', type: 'danger' }
]